﻿'Option Strict On

Imports VB = Microsoft.VisualBasic

Public Class Form2

    ' Use this Random object to choose random icons for the squares
    Private random As New Random

    'Private GameBackColor As Color = Nothing
    'Private GameForeColor As Color = Nothing
    'Private IconBackColor As Color = Nothing
    'Private IconForeColor As Color = Nothing
    Private FormSize As System.Drawing.Size
    Private TempFont As System.Drawing.Font
    Private ListofIconLabels As New List(Of System.Windows.Forms.Label)

    Private StartPause As DateTime
    Private ElapsedPause As TimeSpan
    Private StartTime As DateTime
    Private ElapsedTime As TimeSpan
    Private ImgFilename As String

    Public Property xGameset As Gameset

    Public Sub New()

        ' get user settings
        'IconBackColor = My.Settings.IconBackColor
        'IconForeColor = My.Settings.IconForeColor
        'If IconBackColor.IsEmpty Then IconBackColor = Color.White
        'If IconForeColor.IsEmpty Then IconForeColor = Color.Black

        ' This call is required by Windows Form Designer
        InitializeComponent()

        ' populate form based on saved settings
        'If Not My.Settings.GameSetFormSize.IsEmpty Then Me.Size = FormSize

        GameCardListBox.Items.Clear()
        For Each item In Main.MyGameset.GetGameNames()
            GameCardListBox.Items.Add(item)
        Next
        GameCardListBox.Text = GameCard
        TempFont = GameCardFont

        Dim val1 As String = Main.MyGameset.GameCard
        'Dim val2 As String = xGameset.GameCard
        Stop

        ' collect labels with Tag=icon to be assigned an icon
        Dim iconLabel As Label
        For Each control In TableLayoutPanel1.Controls
            iconLabel = TryCast(control, Label)
            If iconLabel IsNot Nothing AndAlso iconLabel.Tag.ToString.StartsWith("icon") Then
                ListofIconLabels.Add(iconLabel)
            End If
        Next

        AssignColorsToSquares()
        ShowIcons()
    End Sub



    ''' <summary>
    ''' Apply current font to icon squares
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AssignFontToSquares()

        'Stop
        Dim StringDelimiter As String = "|"

        ' letter icons in the Webdings font
        Dim GameCardString As String = "!|N|,|k|b|v|w|z"

        ' repeat the string twice
        GameCardString = GameCardString & StringDelimiter & GameCardString

        ' make it a list
        Dim GameCardArray() As String = GameCardString.Split(CChar(StringDelimiter))
        Dim icons As List(Of String) = GameCardArray.ToList()

        'Dim icons =
        '  New List(Of String) From {"!", "!", "N", "N", ",", ",", "k", "k",
        '                            "b", "b", "v", "v", "w", "w", "z", "z"}

        ' labels with Tag=icon are assigned an icon
        For Each control In TableLayoutPanel1.Controls
            Dim iconLabel = TryCast(control, Label)

            If iconLabel IsNot Nothing AndAlso iconLabel.Tag Is "icon" Then
                iconLabel.ForeColor = IconForeColor
                iconLabel.BackColor = IconBackColor
                iconLabel.Enabled = True

                Dim randomNumber = random.Next(icons.Count)
                iconLabel.Text = icons(randomNumber)
                icons.RemoveAt(randomNumber)
            End If
        Next

    End Sub


    ''' <summary>
    ''' Assign color background to icon squares
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AssignColorsToSquares()
        'Dim img1 As New Bitmap(AppDir & "my photo.jpg")
        'Dim img2 As New Bitmap(img, Label10.Width, Label10.Height)

        Dim brightness As Single
        brightness = IconBackColor.GetBrightness
        If brightness < 0.5 Then
            IconForeColor = Color.White
        Else
            IconForeColor = Color.Black
        End If

        ' TableLayoutPanel labels with Tag=icon are assigned an icon
        For Each control In TableLayoutPanel1.Controls
            Dim iconLabel = TryCast(control, Label)

            If iconLabel IsNot Nothing AndAlso iconLabel.Tag Is "icon" Then
                iconLabel.ForeColor = IconForeColor
                iconLabel.BackColor = IconBackColor

                'img1 = System.Drawing.Image.FromFile(AppDir & "my photo.jpg")
                'img1.Size = iconLabel.Size
                'iconLabel.BackgroundImageLayout = ImageLayout.Stretch
                'iconLabel.BackgroundImage = img
                'iconLabel.BackgroundImage = Nothing
                'iconLabel.Image = img2
                'iconLabel.Image = Nothing
            End If
        Next

    End Sub


    ''' <summary>
    ''' Assign icon squares
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AssignIconsToSquares(ByVal ShowIcons As Boolean)

        'Stop
        Dim icons As List(Of String) = Main.MyGameset.GetListofIcons()
        Dim ListofInt As New List(Of Integer)
        ListofInt = RandomIndex(icons.Count)

        ' labels with Tag=icon are assigned an icon
        For Each control In TableLayoutPanel1.Controls
            Dim iconLabel = TryCast(control, Label)

            If iconLabel IsNot Nothing AndAlso iconLabel.Tag Is "icon" Then
                iconLabel.ForeColor = IconForeColor
                iconLabel.BackColor = IconBackColor
                iconLabel.Enabled = True

                Dim randomNumber = random.Next(icons.Count)
                iconLabel.Text = icons(randomNumber)
                icons.RemoveAt(randomNumber)
            End If
        Next

    End Sub


    ''' <summary>
    ''' Show icons and enable icon squares
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ShowIcons()

        ' TableLayoutPanel labels with Tag=icon are assigned an icon
        For Each control In TableLayoutPanel1.Controls
            Dim iconLabel = TryCast(control, Label)

            If iconLabel IsNot Nothing AndAlso iconLabel.Tag Is "icon" Then
                iconLabel.ForeColor = IconForeColor
                iconLabel.Enabled = True
            End If
        Next

    End Sub


    ''' <summary>
    ''' assign image or text to clicked icon square
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub label_Click(ByVal sender As System.Object,
                            ByVal e As System.EventArgs) Handles IconLabelR1C4.Click, IconLabelR1C3.Click, IconLabelR1C2.Click, IconLabelR1C1.Click,
        IconLabelR2C4.Click, IconLabelR2C3.Click, IconLabelR2C2.Click, IconLabelR2C1.Click

        Dim iconLabel = TryCast(sender, Label)
        If iconLabel IsNot Nothing And Microsoft.VisualBasic.Left(iconLabel.Tag, 4) = "icon" Then
            If TextGameType.Checked Then
                'get text string
                Dim txt As String = InputBox("Enter text string", "Match String", "")

                'display it
                iconLabel.Text = txt
                iconLabel.BackgroundImage = Nothing

                Stop
            ElseIf ImagesGameType.Checked Then
                Dim ImgCount As Integer = 1
                Dim ImgFilenames() As String

                ImgFilenames = GetIconsFromFiles(1, "")
                ImgCount = ImgFilenames.Count()
                Stop
                'Set the Filter.
                OpenFileDialog1.Title = "Select an image file"
                OpenFileDialog1.Filter = "Bitmap|*.bmp|JPG|*.jpg|GIF|*.gif|All Files|*.*"
                OpenFileDialog1.FilterIndex = 2
                OpenFileDialog1.DefaultExt = "JPG"
                OpenFileDialog1.Multiselect = False
                'OpenFileDialog1.InitialDirectory = ""
                OpenFileDialog1.FileName = iconLabel.Tag


                'Show it
                If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
                    'Get the image name
                    ImgFilename = OpenFileDialog1.FileName
                    Stop
                    'ImgFilename = OpenFileDialog1.SafeFileName

                    'Create a new Bitmap and display it
                    iconLabel.BackgroundImageLayout = ImageLayout.Zoom
                    iconLabel.BackgroundImage = System.Drawing.Bitmap.FromFile(ImgFilename)
                End If
            Else
            End If
        End If
    End Sub


    Private Sub GameCardListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GameCardListBox.SelectedIndexChanged
        GameCard = Me.GameCardListBox.Text
        Call Main.MyGameset.LoadGameCard(GameCard)
        Call AssignIconsToSquares(True)
        My.Settings.GameCard = GameCard

        If GameCard = "xxxDefault" Then
            Me.GameCardListBox.DropDownStyle = ComboBoxStyle.DropDownList
        Else
            Me.GameCardListBox.DropDownStyle = ComboBoxStyle.DropDown
        End If
    End Sub


    ''' <summary>
    ''' add new game card to list
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddGameButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddGameButton.Click, UpdateGameButton.Click
        Dim NewGameCard As String
        Dim PlayerExists As Integer

        NewGameCard = InputBox("Enter name of game card", "New Game Card", "")
        If NewGameCard Is "" Then Exit Sub

        PlayerExists = GameCardListBox.FindString(NewGameCard)
        If PlayerExists = -1 Then
            GameCardList.Add(NewGameCard)
            GameCardListBox.Items.Add(NewGameCard)
            Dim tmp As Integer = GameCardList.Count
            My.Settings.PlayerList = GameCardList
            GameCard = NewGameCard
        Else
            GameCard = CStr(GameCardListBox.Items.Item(PlayerExists))
        End If

        If Not GameCardListBox.Text Is GameCard Then
            GameCardListBox.Text = GameCard
            Call GameCardListBox_SelectedIndexChanged(Me.GameCardListBox, Nothing)
        End If
        'Stop
    End Sub


    ''' <summary>
    ''' remove existing game card from list
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub RemoveGameButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveGameButton.Click
        GameCard = GameCardListBox.Text
        GameCardList.Remove(GameCard)
        GameCardListBox.Items.Remove(GameCard)

        My.Settings.GameCardList = GameCardList
        My.Settings.GameCard = GameCard

        If GameCardList.Count > 0 Then
            GameCard = CStr(GameCardListBox.Items(1))
        Else
            GameCard = ""
        End If
        GameCardListBox.Text = GameCard
        'Stop

    End Sub


    ''' <summary>
    ''' add selected text or images to icon squares
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddItemsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddItemsButton.Click

        Dim ImgCount As Integer = 1
        Dim ImgCountMax As Integer = CInt(IconSquareCount / 2)
        Dim ImgFilenames() As String

        ImgFilenames = GetIconsFromFiles(ImgCountMax, "")
        ImgCount = ImgFilenames.Count()
        If ImgCount = 0 Then Exit Sub
        Stop

        'Set the Open File dialog options
        OpenFileDialog1.Title = "Select up to 8 image files"
        OpenFileDialog1.Filter = "BMP|*.bmp|JPG|*.jpg|GIF|*.gif"
        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.DefaultExt = "JPG"
        OpenFileDialog1.Multiselect = True
        OpenFileDialog1.FileName = ""

        'Show dialog
        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
            'Get the image name
            ImgFilenames = OpenFileDialog1.FileNames
            If ImgFilenames.Count > ImgCountMax Then
                MsgBox("Only " & ImgCountMax & " filenames will be used")
            Else
                ImgCountMax = ImgFilenames.Count
            End If
        Else
            Exit Sub
        End If

        Call ClearItemsButton_Click(Nothing, Nothing)
        'Stop

        ' TableLayoutPanel labels with Tag=icon are icon squares
        For Each control In TableLayoutPanel1.Controls
            Dim iconLabel = TryCast(control, Label)

            If iconLabel IsNot Nothing Then
                'Stop
                If VB.Left(iconLabel.Tag, 4) = "icon" Then
                    'iconLabel.BackgroundImage = Nothing
                    'iconLabel.Text = ""
                    'iconLabel.Tag = "icon"
                    If ImgCount <= ImgCountMax Then
                        ImgFilename = ImgFilenames(ImgCount)
                        'Stop
                        iconLabel.BackgroundImageLayout = ImageLayout.Zoom
                        iconLabel.BackgroundImage = System.Drawing.Bitmap.FromFile(ImgFilename)
                        iconLabel.Tag = "icon_" & ImgFilename
                        ImgCount = ImgCount + 1
                    End If
                End If
            End If
        Next

    End Sub


    ''' <summary>
    ''' clear text or images from icon squares
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ClearItemsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearItemsButton.Click

        ' TableLayoutPanel labels with Tag=icon are icon squares
        For Each control In TableLayoutPanel1.Controls
            Dim iconLabel = TryCast(control, Label)

            If iconLabel IsNot Nothing Then
                If Microsoft.VisualBasic.Left(iconLabel.Tag, 4) Is "icon" Then
                    iconLabel.BackgroundImage = Nothing
                    iconLabel.Text = ""
                    iconLabel.Tag = "icon"
                End If
            End If
        Next

    End Sub

    Private Function GetIconsFromFiles(ByVal MaxFiles As Integer, ByVal DefaultFile As String) As String()

        Dim ImgFilenames(-1) As String
        'Dim ImgFilenames As ArrayList

        'Set the Open File dialog options
        If MaxFiles > 1 Then
            OpenFileDialog1.Multiselect = True
            OpenFileDialog1.Title = "Select up to " & MaxFiles & " image files"
            OpenFileDialog1.FileName = ""
        Else
            OpenFileDialog1.Multiselect = False
            OpenFileDialog1.Title = "Select image file"
            OpenFileDialog1.FileName = DefaultFile
        End If
        OpenFileDialog1.Filter = "BMP|*.bmp|JPG|*.jpg|GIF|*.gif"
        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.DefaultExt = "JPG"

        'Show dialog
        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
            'Get the image name
            ImgFilenames = OpenFileDialog1.FileNames
            Stop
            If ImgFilenames.Count > MaxFiles Then
                MsgBox("Only " & MaxFiles & " selected filenames will be used")
            Else
                MaxFiles = ImgFilenames.Count
            End If
            Return ImgFilenames
        Else
            'Return Nothing
            Return ImgFilenames
        End If

    End Function

    Private Sub SetFontButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetFontButton.Click
        If TextGameType.Checked Then
            FontDialog1.Font = TempFont
            If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                TempFont = FontDialog1.Font
            End If
            ' TableLayoutPanel labels with Tag=icon are icon squares
            For Each control In TableLayoutPanel1.Controls
                Dim iconLabel = TryCast(control, Label)

                If iconLabel IsNot Nothing Then
                    'Stop
                    If VB.Left(iconLabel.Tag, 4) = "icon" Then
                        iconLabel.Font = TempFont
                    End If
                End If
            Next

        ElseIf ImagesGameType.Checked Then
        Else
            Exit Sub
        End If
    End Sub

End Class
